#include "JY61_P.h"

//定义结构体名
struct StcAngle SAngle;
struct StcGyro  SGyro;
struct StcAcc   SAcc;

void JY61_Init(uint32_t bound)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);

    GPIO_PinAFConfig(GPIOD,GPIO_PinSource6,GPIO_AF_USART2);//IO口用作串口引脚要配置复用模式
    GPIO_PinAFConfig(GPIOD,GPIO_PinSource5,GPIO_AF_USART2);

    GPIO_StructInit(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin           = GPIO_Pin_5;//TX引脚
    GPIO_InitStructure.GPIO_Mode          = GPIO_Mode_AF;//IO口用作串口引脚要配置复用模式
    GPIO_InitStructure.GPIO_Speed         = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_OType         = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd          = GPIO_PuPd_UP;
    GPIO_Init(GPIOD,&GPIO_InitStructure);

    GPIO_StructInit(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin           = GPIO_Pin_6;//RX引脚
    GPIO_InitStructure.GPIO_Mode          = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed         = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_OType         = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd          = GPIO_PuPd_UP;
    GPIO_Init(GPIOD,&GPIO_InitStructure);

    USART_InitTypeDef USART_InitStructure;//定义配置串口的结构体变量

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);//开启串口二的时钟

    USART_DeInit(USART2);//清除TIM2的相关配置

    USART_StructInit(&USART_InitStructure);
    USART_InitStructure.USART_BaudRate              = (uint32_t)(115200);//设置波特率
    USART_InitStructure.USART_WordLength            = USART_WordLength_8b;//字节长度为8bit
    USART_InitStructure.USART_StopBits              = USART_StopBits_1;//1个停止位
    USART_InitStructure.USART_Parity                = USART_Parity_No ;//没有校验位
    USART_InitStructure.USART_Mode                  = USART_Mode_Rx | USART_Mode_Tx;//将串口配置为收发模式
    USART_InitStructure.USART_HardwareFlowControl   = USART_HardwareFlowControl_None; //不提供流控
    USART_Init(USART2,&USART_InitStructure);//将相关参数初始化给串口二

    NVIC_InitTypeDef NVIC_InitStructure;//中断控制结构体变量定义
    NVIC_InitStructure.NVIC_IRQChannel                    = USART2_IRQn;//中断通道指定为USART2
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority  = 0;//主优先级为0
    NVIC_InitStructure.NVIC_IRQChannelSubPriority         = 1;//次优先级为1
    NVIC_InitStructure.NVIC_IRQChannelCmd                 = ENABLE;//确定使能
    NVIC_Init(&NVIC_InitStructure);//初始化配置此中断通道

    USART_ClearFlag(USART2,USART_FLAG_RXNE);//初始配置时清除接受置位

    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//初始配置接受中断

    USART_Cmd(USART2,ENABLE);//开启串口二
}


/******** 串口二 中断服务函数 ***********/
void USART2_IRQHandler(void)
{
    if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET)//判断是不是真的有中断发生
    {
        //USART_SendData(USART2,USART_ReceiveData(USART2));//又将数据发回去(用于验证)
        //Get_Jy61(USART_ReceiveData(USART2));
        uint16_t Data=(USART2->DR & (uint16_t)0x01FF);
        Get_Jy61(Data);
    }
    USART_ClearITPendingBit(USART2, USART_IT_RXNE); //清除标志位
}

/***********采集JY61P数据**************/
void Get_Jy61(uint16_t Data)
{
    static u8 RX_Data[250];
    static u8 len = 0;
    RX_Data[len++]=Data;    //将收到的数据存入缓冲区中
    if (RX_Data[0]!=0x55) //数据头不对，则重新开始寻找0x55数据头
    {
        len=0;
        return;
    }
    if (len<11) return;//数据不满11个，则返回
    else
    {
     switch(RX_Data[1])//判断数据是哪种数据，然后将其拷贝到对应的结构体中，有些数据包需要通过上位机打开对应的输出后，才能接收到这个数据包的数据
     {
        case 0x51:
                        memcpy(&SAcc,&RX_Data[2],8);
                        for(uint8_t j = 0; j < 3; j++)
                            {
                                SAcc.Acc[j] = (float)SAcc.Acc_S[j]/32768*16;
                            }
                        break;
        case 0x52:
                        memcpy(&SGyro,&RX_Data[2],8);
                        for(uint8_t j = 0; j < 3; j++)
                            {
                                SGyro.Gyro[j] = (float)SGyro.Gyro_S[j]/32768*2000;
                            }
                        break;
        case 0x53:
                        memcpy(&SAngle,&RX_Data[2],8);
                        for(uint8_t j = 0; j < 3; j++)
                            {
                               SAngle.Angle[j] = (float)SAngle.Angle_S[j]/32768*180;
                            }
                        break;
     }
     len=0;//清空缓存区
 }
}
